package TankGame1;

import java.io.Serializable;

/**
 * 子弹
 */
public class Shot implements Runnable , Serializable {
    private int x; //子弹X位置
    private int y; //子弹Y位置
    private int speed; //子弹速度
    private int direction; //子弹方向
    private boolean isLive=true; //子弹是否存活

    public Shot(int x, int y, int speed, int direction) {
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.direction = direction;
    }

    @Override
    public void run() {
        while(true){ //子弹活着才刷新子弹状态
            //每隔0.1秒子弹就刷新位置
            switch (direction){
                case 0:
                    y-=speed; //Y变小就是往上走
                    break;
                case 1:
                    x+=speed; //X变大就是往右
                    break;
                case 2:
                    y+=speed; //Y变大就是往下
                    break;
                case 3:
                    x-=speed; //X变小时往左
                    break;
            }
            if(x<0||x>1000||y<0||y>750){
                isLive=false;
                //此时说明子弹不在范围内，线程可以结束
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getDirection() {
        return direction;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public boolean isLive() {
        return isLive;
    }
}
